#include <cv.h>
#include <highgui.h>

#include <LoadableImage.h>
#include <ClassificationManager.h>
#include <ClassificationParams.h>
#include <Check.h>


#include <iostream>
#include <fstream>

using namespace RandomForest;
using namespace std;

void onTrackbar( int value, void* rawParam)
{
	const double curValue = (double) value/100 ;
	
	double* param = static_cast<double*>( rawParam );
	*param = curValue;
}
int main ( int argc, char **argv )
{
	CoInitialize( 0 );

	ofstream file;
	file.open ("E:\\1.txt");
	cout.rdbuf( file.rdbuf() );


	CClassificationParams classificationParams;
	classificationParams.FeaturePointsCount = 200;
	classificationParams.HalfPatchSize = 10;
	classificationParams.InitialImagePath = "E:\\Laboratory\\RandomForest\\train.jpg";
	classificationParams.MaxTreeHeight = 30;
	classificationParams.MinClassificationConfedence = 0.1;
	classificationParams.MinElementPerNode = 100;
	classificationParams.RandomTreesCount = 30;
	classificationParams.TrainingBaseFolder = "E:\\Laboratory\\RandomForest\\BookFeatures\\";
	classificationParams.ClassifierIntensityThreshold = 10;
	classificationParams.GeneratedRandomPointsCount = 30;

	CClassificationManager manager( classificationParams );
	//manager.GenerateTrainingBase();

	//manager.WriteTrainingBaseToFolder();

	manager.LoadTrainingBaseFromFolder();

	manager.TrainClassifier();

	CLoadableImage initialImage( cvLoadImage( classificationParams.InitialImagePath.c_str(), CV_LOAD_IMAGE_GRAYSCALE ) );

	//////////////////////////
	CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );

	if ( !capture ) {
		std::cout << "ERROR: capture is NULL \n" << std::endl;
		return -1;
	}

	// Create a window in which the captured images will be presented
	cvNamedWindow( "classification", CV_WINDOW_FULLSCREEN );

	cv::createTrackbar( "min confedence", "classification", 0, 100, onTrackbar, &classificationParams.MinClassificationConfedence  );

	// Show the image captured from the camera in the window and repeat
	while ( 1 ) {
		// Get one frame
		IplImage* frame = cvQueryFrame( capture );
		if ( !frame ) {
			break;
		}

		CLoadableImage imageToSave( cvCreateImage( cvGetSize( frame ), IPL_DEPTH_8U, 1 ) );
		cvCvtColor( frame, imageToSave, CV_RGB2GRAY );

		CLoadableImage correctedFrame( cvCloneImage( imageToSave ) );
		cvSmooth( imageToSave, correctedFrame, CV_GAUSSIAN, 7, 7 );

		//CLoadableImage correctedFrame2( cvCloneImage( correctedFrame ) );
		//cvEqualizeHist( correctedFrame, correctedFrame2 );

		CLoadableImage imageToShow = manager.ClassifyImage( initialImage, correctedFrame );

		cvShowImage( "classification", imageToShow );
		// Do not release the frame!
		//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
		//remove higher bits using AND operator
		if ( (cvWaitKey(10) & 255) == 27 ) break;
	}
	// Release the capture device housekeeping
	cvReleaseCapture( &capture );
	cvDestroyWindow( "classification" );


	CoUninitialize();

	return 0;
}
